package com.ucap.weking.guangan.Activitys;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.boredream.bdvideoplayer.utils.DisplayUtils;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.adapter.ViewPagerAdapter;
import com.ucap.weking.guangan.fragments.liveOnlineFrgmentContent.FragmentContent1;
import com.ucap.weking.guangan.fragments.liveOnlineFrgmentContent.FragmentContent4;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 访谈直播
 */
public class LiveOnlineActivity extends AppCompatActivity {

    @InjectView(R.id.wb_content)
    WebView wbContent;
    @InjectView(R.id.ll_root)
    LinearLayout llRoot;
    @InjectView(R.id.vp_content)
    ViewPager vpContent;
    @InjectView(R.id.table)
    TabLayout tabLayout;
    @InjectView(R.id.ll_bottom_root)
    LinearLayout llBottomRoot;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.iv_screen)
    ImageView ivScreen;
    String videoUri;
    private Bundle liveData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_online2);
        ButterKnife.inject(this);
        liveData = getIntent().getBundleExtra("live_data");
        videoUri = liveData.getString("video");
        intiData();
        intiFooter();
        intiListener();
    }

    private void intiListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DisplayUtils.isPortrait(getApplicationContext())) {
                    onBackPressed();
                } else if (!DisplayUtils.isPortrait(getApplicationContext())) {
                    isGon(false);
                }
            }
        });
        ivScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isGon(DisplayUtils.isPortrait(getApplicationContext()));
            }
        });


    }

    @Override
    public void onBackPressed() {
        if (DisplayUtils.isPortrait(getApplicationContext())) {
            super.onBackPressed();
        } else if (!DisplayUtils.isPortrait(getApplicationContext())) {
            isGon(false);
        }

    }

    public void isGon(boolean state) {
        if (state) {
            ivScreen.setImageResource(R.drawable.ic_screen_min);
            llBottomRoot.setVisibility(View.GONE);
        } else {
            ivScreen.setImageResource(R.drawable.ic_screen_max);
            llBottomRoot.setVisibility(View.VISIBLE);
        }
        DisplayUtils.toggleScreenOrientation(LiveOnlineActivity.this);
    }

    private void intiData() {
        Log.i("直播地址", "intiData: " + videoUri);
        WebSettings ws = wbContent.getSettings();
        ws.setBuiltInZoomControls(false);// 隐藏缩放按钮
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);// 排版适应屏幕
        ws.setUseWideViewPort(false);// 可任意比例缩放
        ws.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。
        ws.setSavePassword(true);
        ws.setSaveFormData(true);// 保存表单数据
        ws.setJavaScriptEnabled(true);
        ws.setGeolocationEnabled(true);// 启用地理定位
        ws.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");// 设置定位的数据库路径
        ws.setDomStorageEnabled(true);
        wbContent.setWebChromeClient(new WebChromeClient());
//        wbContent.loadUrl("http://baobab.wdjcdn.com/145076769089714.mp4");
        wbContent.loadUrl(videoUri);
    }

    private void intiFooter() {
        ArrayList<Fragment> data = new ArrayList<>();
        data.add(new FragmentContent4().setDataText(liveData));
        data.add(new FragmentContent1());
        vpContent.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), data, new String[]{"直播简介", "网友心声"}));
        tabLayout.setupWithViewPager(vpContent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wbContent.destroy();
        llRoot.removeView(wbContent);
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
